<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\service\cdc\sync;

use app\enum\CdcEnum;
use app\model\mongo\MongoAppendSyncModel;
use app\model\mongo\MongoReportModel;
use app\model\mongo\MongoSyncModel;
use app\service\cdc\VideoService;

abstract class VideoLogService extends LogService
{
    /**
     * 删除当前日志.
     * @param MongoSyncModel $model 当前日志模型
     */
    public static function deleteLogByModel(MongoSyncModel $model): bool
    {
        return VideoService::deleteSyncLog($model, CdcEnum::LOG_DELETE_SELF);
    }

    /**
     * 删除指定账户素材日志.
     * @param MongoSyncModel $model 当前日志模型
     */
    public static function deleteLogByMaterialId(MongoSyncModel $model): bool
    {
        return VideoService::deleteSyncLog($model, CdcEnum::LOG_DELETE_MATERIAL);
    }

    /**
     * 新增日志.
     * @param MongoReportModel $model 记录表模型
     */
    public static function addLogByRecord(MongoReportModel $model): bool
    {
        $obj = static::getModel();
        $field = [
            'client_id' => $model->getAttr('client_id'),
            'account_id' => $model->getAttr('account_id'),
            'stat_date' => $model->getAttr('stat_date'),
            'type' => $model->getAttr('type'),
            'user_id' => static::getUserIdByAdvId($model->getAttr('adv_id') ?? '') ?? '',
        ];

        return VideoService::addSyncLog($obj, $field);
    }

    /**
     * 删除日志.
     * @param MongoSyncModel $model 中间表模型
     * @param int $statDate 汇总表版本
     */
    public static function deleteLog(MongoSyncModel $model, int $statDate = 0): bool
    {
        // 追加模型数据删除
        if ($model instanceof MongoAppendSyncModel && $model->getKey()) {
            return self::deleteLogByAppendModel($model);
        }

        if (! $statDate) {
            return self::deleteLogByModel($model);
        }
        return self::deleteLogByMaterialId($model);
    }

    /**
     * 删除日志.
     * @param MongoAppendSyncModel $model 中间表模型
     */
    public static function deleteLogByAppendModel(MongoAppendSyncModel $model): bool
    {
        return VideoService::deleteAppendSyncLog($model);
    }

    /**
     * 获取信息.
     * @param array $where 条件
     * @return mixed
     */
    public static function getInfo(array $where = [])
    {
        return static::getModel()->where($where)->field(true)->find();
    }
}
